using System;
using System.Collections.Generic;
using System.Text;
using SideShow.Driver.ComInterop;
using System.Runtime.InteropServices;

namespace SideShow.Driver
{
    [ComVisible(true)]
    [Guid("B4C2C8D0-1BFB-41e7-8328-2C68DD9F212E")]
    public class SideShowQueue<T, R> : 
        IQueueCallbackDeviceIoControl where T : SideShowDevice<R> where R : SideShowDriver, new()
    {
        private T _parentDevice;

        public SideShowQueue(T parentDevice)
        {
            _parentDevice = parentDevice;
        }

        #region IQueueCallbackDeviceIoControl Members

        public void OnDeviceIoControl(IWDFIoQueue pWdfQueue, IWDFIoRequest pWdfRequest, uint ControlCode, uint InputBufferSizeInBytes, uint OutputBufferSizeInBytes)
        {
            uint written = 0;
            int hr = 0;
            if (NativeMethods.IsWpdIoCtl(ControlCode))
            {
                try
                {
                    _parentDevice.ProcessIoControl(pWdfQueue, pWdfRequest, ControlCode, InputBufferSizeInBytes, OutputBufferSizeInBytes, out written);
                }
                catch (COMException ex)
                {
                    hr = ex.ErrorCode;
                }
                pWdfRequest.CompleteWithInformation(hr, written);
            }
            else
            {
                hr = ComHResults.HResultFromWin32(NativeMethods.ERROR_NOT_SUPPORTED);
                pWdfRequest.CompleteWithInformation(hr, written);
            }
        }

        #endregion
    }
}
